#!/usr/bin/ev python3
# -*- coding: utf-8 -*-
# Desc: unittest mock

import unittest
from unittest.mock import MagicMock

# Part 1
class A(unittest.TestCase):
    """
    1. 单测函数m1，m1调用m2\m3，如果m2\m3内部较为复杂，此时就可以考虑mock
    2. 因为m1中val需要m2的一个返回值，故通过mock构造一个指定的返回值`a.m2 = MagicMock(return_value='custom_val01')`
    3. m3直接用MagicMock替代即可
    """
    def m1(self):
        val = self.m2()
        self.m3(val)

    def m2(self):
        pass

    def m3(self, val):
        pass

    def test_m1(self):
        a = A()
        a.m2 = MagicMock(return_value='custom_val01')
        a.m3 = MagicMock()
        a.m1()
        self.assertTrue(a.m2.called)
        a.m3.assert_called_with('custom_val')


if __name__ == '__main__':
    unittest.main()

# Part 2

# def side_effect(arg):
#     if arg < 0:
#         return 1
#     else:
#         return 2
#
# mock = MagicMock()
# mock.side_effect = side_effect
#
# print(mock(-1), mock(10))

